package cn.jiangxiong.pethome.system.user.controller;

import cn.jiangxiong.pethome.system.user.domain.Employee;
import cn.jiangxiong.pethome.system.user.query.EmployeeQuery;
import cn.jiangxiong.pethome.system.user.service.IEmployeeService;
import cn.jiangxiong.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")//说明该类的作用
public class EmployeeController {
     @Autowired
    private IEmployeeService iEmployeeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有的员工信息" )
     public List<Employee> findAll(){
         return iEmployeeService.findAll();
     }

     @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工信息")
    public AjaxResult findOne(@PathVariable Long id){
         try {
             Employee one = iEmployeeService.findOne(id);
             return AjaxResult.success().setData(one);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return AjaxResult.error();
     }

     @PostMapping("/pagelist")
    @ApiOperation(value = "员工列表分页查询")
    public AjaxResult findQuery(@RequestBody EmployeeQuery query){
         try {
             List<Employee> byQuery = iEmployeeService.findByQuery(query);
             Long count = iEmployeeService.findByQueryCount(query);
             return  AjaxResult.success().setData(byQuery).setTotal(count);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return AjaxResult.error();
     }

     @DeleteMapping("/{id}")
    @ApiOperation(value = "根据员工id删除数据")
    public AjaxResult delete(@PathVariable Long id){
         try {
             iEmployeeService.delete(id);
             return AjaxResult.success().setMsg("删除成功");
         } catch (Exception e) {
             e.printStackTrace();
             return AjaxResult.error().setMsg(e.getMessage());
         }

     }
     @PostMapping
    @ApiOperation(value = "保存员工信息")
    public AjaxResult save(@RequestBody Employee employee){
         try {
             if (employee.getId() != null){
                 iEmployeeService.update(employee);

             }else {
                 iEmployeeService.add(employee);
             }
             return AjaxResult.success();
         } catch (Exception e) {
             e.printStackTrace();
             return AjaxResult.error().setMsg(e.getMessage());
         }
     }
     @PatchMapping("/del")
     @ApiOperation(value = "根据id批量删除数据")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
         try {
             iEmployeeService.batchDel(ids);
             return AjaxResult.success().setMsg("删除成功");
         } catch (Exception e) {
             e.printStackTrace();
         }
         return AjaxResult.error().setMsg("删除失败");
     }









}
